package com.tutu.hosp.controller.api;

import cn.hutool.core.util.StrUtil;
import com.tutu.common.exception.YyghException;
import com.tutu.common.helper.HttpRequestHelper;
import com.tutu.common.result.Result;
import com.tutu.common.result.ResultCodeEnum;
import com.tutu.common.utils.MD5;
import com.tutu.hosp.service.DepartmentService;
import com.tutu.hosp.service.HospitalService;
import com.tutu.hosp.service.HospitalSetService;
import com.tutu.hosp.service.ScheduleService;
import com.tutu.model.hosp.Department;
import com.tutu.model.hosp.Hospital;
import com.tutu.model.hosp.Schedule;
import com.tutu.vo.hosp.DepartmentQueryVo;
import com.tutu.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Program: yygh_parent
 * @Description: 上传医院科室外部接口
 * @Author: JK.TuTu
 * @Create: 2022-09-22 22:43
 **/
@Slf4j
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 上传医院接口
     *
     * @param httpServletRequest
     * @return
     */
    @ApiOperation("上传医院接口")
    @PostMapping("/saveHospital")
    public Result saveHosp(HttpServletRequest httpServletRequest) {
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院系统传递来的签名并对签名进行MD5加密
        String hospSign = (String) objectMap.get("sign");
        //获取传递过来的医院编码 查询数据库 拆查询签名
        String hoscode = (String) objectMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查询的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中将"+"转换成了" "  我们要转换回来
        String logoDataString = (String) objectMap.get("logoData");
        if (!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            objectMap.put("logoData", logoData);
        }
        hospitalService.save(objectMap);
        return Result.ok();
    }


    @ApiOperation("查询医院接口")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院系统传递来的签名并对签名进行MD5加密
        String hospSign = (String) objectMap.get("sign");
        //获取传递过来的医院编码 查询数据库 查询询签名
        String hoscode = (String) objectMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查询的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service中的方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    /**
     * 上传医院科室接口
     *
     * @param request
     * @return
     */
    @ApiOperation("上传科室接口")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(parameterMap);
        //医院编号hoscode不为空校验
        String hoscode = (String) objectMap.get("hoscode");
        if (StrUtil.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        String hospSign = (String) objectMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(objectMap);
        return Result.ok();
    }

    /**
     * 分页查询医院科室接口
     *
     * @param request
     * @return
     */
    @ApiOperation("分页查询医院科室接口")
    @PostMapping("department/list")
    public Result selectForPage(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(parameterMap);
        //获取传递过来的参数
        String hoscode = (String) objectMap.get("hoscode");
        if (StrUtil.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        String hospSign = (String) objectMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String depcode = (String) objectMap.get("depcode");
        Integer page = StrUtil.isEmpty((String) objectMap.get("page")) ? 1 : Integer.parseInt((String) objectMap.get("page"));
        Integer limit = StrUtil.isEmpty((String) objectMap.get("limit")) ? 10 : Integer.parseInt((String) objectMap.get("limit"));

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);
        Page<Department> pageModel = departmentService.selectPage(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 删除医院科室接口
     *
     * @param request
     * @return
     */
    @ApiOperation("删除医院科室信息")
    @RequestMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(parameterMap);
        //获取传递过来的参数
        String hoscode = (String) objectMap.get("hoscode");
        if (StrUtil.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        String hospSign = (String) objectMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String depcode = (String) objectMap.get("depcode");
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    /**
     * 上传排班信息接口
     *
     * @param request
     * @return
     */
    @ApiOperation("上传排班信息接口")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(parameterMap);
        //获取传递过来的参数
        String hoscode = (String) objectMap.get("hoscode");
        if (StrUtil.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        String hospSign = (String) objectMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(objectMap);
        return Result.ok();
    }


    @ApiOperation("分页查询排班信息接口")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(parameterMap);
        //获取传递过来的参数
        String hoscode = (String) objectMap.get("hoscode");
        if (StrUtil.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        String hospSign = (String) objectMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String depcode = (String) objectMap.get("depcode");
        Integer page = StrUtil.isEmpty((String) objectMap.get("page")) ? 1 : Integer.parseInt((String) objectMap.get("page"));
        Integer limit = StrUtil.isEmpty((String) objectMap.get("limit")) ? 10 : Integer.parseInt((String) objectMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = scheduleService.selectPage(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("删除排班信息")
    @RequestMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(parameterMap);
        //获取传递过来的参数
        String hoscode = (String) objectMap.get("hoscode");
        if (StrUtil.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        String hospSign = (String) objectMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String hosScheduleId = (String)objectMap.get("hosScheduleId");
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }


}
